<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Upload;
class QRcodeController extends Controller {
    public function index(){
        $this->display();
    }

    public function temp_qrcode(){
        $back_x = $_GET['back_x'];
        $back_y = $_GET['back_y'];
        $qrcode_size = $_GET['qrcode_size'];
        if (empty($qrcode_size)) {
            $qrcode_size = 8;
        }

        $rootPath = C('TMPL_PARSE_STRING.__UPLOAD__');
        $domain_url = C('TMPL_PARSE_STRING.__DOMAINURL__');
        $root = C('TMPL_PARSE_STRING.__WEBSITEDIR__');

        $qRcode_path = C('TMPL_PARSE_STRING.__QRCODEBGPATH__');
        $aRcode_name = C('TMPL_PARSE_STRING.__QRCODEBGNAME__');

        $logo = $rootPath.$qRcode_path."/".$aRcode_name.".jpg";//已经生成的原始二维码图
        $QR = $rootPath.$qRcode_path."/temp_QRCode.png";//已经生成的原始二维码图
        qrcode($domain_url.$root,$qrcode_size, $QR);

        $image_1 = imagecreatefromstring(file_get_contents($logo));
        $image_2 = imagecreatefromstring(file_get_contents($QR));
        //创建一个和人物图片一样大小的真彩色画布（ps：只有这样才能保证后面copy装备图片的时候不会失真）
        $image_3 = imageCreatetruecolor(imagesx($image_1), imagesy($image_1));
        //为真彩色画布创建白色背景，再设置为透明
        // $color = imagecolorallocate($image_3, 255, 255, 255);
        $color = imagecolorallocate($image_3, 0, 0, 255);
        imagefill($image_3, 0, 0, $color);
        imageColorTransparent($image_3, $color);
        //首先将人物画布采样copy到真彩色画布中，不会失真
        imagecopyresampled($image_3, $image_1, 0, 0, 0, 0, imagesx($image_1), imagesy($image_1), imagesx($image_1), imagesy($image_1));
        //再将装备图片copy到已经具有人物图像的真彩色画布中，同样也不会失真
        imagecopymerge($image_3, $image_2, $back_x, $back_y, 0, 0, imagesx($image_2), imagesy($image_2), 99);

        //将画布保存到指定的gif文件
        imagepng($image_3);
    }

    public function qrcode($user_id, $isBg=0) {
        if ($isBg == 0) {
            $modelObj = M("User");
            $data = $modelObj->field("code")->where(array('id' => $user_id))->find();
            $code = $data["code"];

            $modelObj = M('Sys');
            $data = $modelObj->field("qrcode_url")->where(array('id' => 1))->find();
            $url = $data['qrcode_url'].$code;
            qrcode($url,9);
        } else {
            $modelObj = M('User');
            $data = $modelObj->field("code")->where(array('id' => $user_id))->find();
            $code = $data["code"];

            // $domain_url = C('TMPL_PARSE_STRING.__DOMAINURL__');
            // $web_site_dir = C('TMPL_PARSE_STRING.__WEBSITEDIR__');

            $modelObj = M('Sys');
            $data = $modelObj->field("qrcode_url,qrcode_size,back_x,back_y")->where(array('id' => 1))->find();
            $url = $data['qrcode_url'].$code;//str_replace("{#code}",$code,$data['qrcode_url']);

            $rootPath = C('TMPL_PARSE_STRING.__UPLOAD__');
            $qRcode_path = C('TMPL_PARSE_STRING.__QRCODEBGPATH__');
            $aRcode_name = C('TMPL_PARSE_STRING.__QRCODEBGNAME__');

            $logo = $rootPath.$qRcode_path."/".$aRcode_name.".jpg";//已经生成的原始二维码图
            $QR = $rootPath.$qRcode_path."/u".session("user_id")."_QRCode.jpg";//已经生成的原始二维码图

            qrcode($url,$data['qrcode_size'], $QR);

            $image_1 = imagecreatefromjpeg($logo);
            $image_2 = imagecreatefrompng($QR);
            //创建一个和人物图片一样大小的真彩色画布（ps：只有这样才能保证后面copy装备图片的时候不会失真）
            $image_3 = imageCreatetruecolor(imagesx($image_1), imagesy($image_1));
            //为真彩色画布创建白色背景，再设置为透明
            // $color = imagecolorallocate($image_3, 255, 255, 255);
            $color = imagecolorallocate($image_3, 0, 0, 255);
            imagefill($image_3, 0, 0, $color);
            imageColorTransparent($image_3, $color);
            //首先将人物画布采样copy到真彩色画布中，不会失真
            imagecopyresampled($image_3, $image_1, 0, 0, 0, 0, imagesx($image_1), imagesy($image_1), imagesx($image_1), imagesy($image_1));
            //再将装备图片copy到已经具有人物图像的真彩色画布中，同样也不会失真
            imagecopymerge($image_3, $image_2, $data['back_x'], $data['back_y'], 0, 0, imagesx($image_2), imagesy($image_2), 99);

            //将画布保存到指定的gif文件
            imagepng($image_3);
        }

    }
}